//
//  ShengChengFaHuoDanViewController.m
//  FactorySale
//
//  Created by 李 on 2020/1/8.
//  Copyright © 2020 主事丫环. All rights reserved.
//
/**
 界面思路：
 1 顶部由控件拼装
 2 主控件是一个tableview
 3 三种cell 产品名称 ChanPinXingHaoTopViewCell，产品信息列表ShengChengFaHuoDanListCell，合计金额ChanPinXingHaoTotalMoneyCell，剩余的是tablefooterview
 4 在 ShengChengFaHuoDanListCell中修改数量和单价通过block传值到此控制器，在 countTotalMoney方法中计算合计金额
 5 点击订购方未付款 通过改变view的frame以及控件的hidden属性来隐藏或显示收款账户选择项。
 */

#import "ShengChengFaHuoDanViewController.h"
#import "ChanPinXingHaoTopViewCell.h"// 产品名称cell
#import "ChanPinXingHaoTotalMoneyCell.h"// 合计金额cell
#import "WeiFaHuoOrderDetailModel.h"// 未发货订单详情的model
#import "SelectConditionTableView.h"// 这是一个选择视图，不是真正意义上的表
#import "FactoryBankCardsListModel.h"// 银行卡列表model
#import "WuLiuListModel.h"// 物流信息model
#import "ShengChengFaHuoDanListCell.h"// 生成发货单
#import "BankCardManageViewController.h"// 银行卡管理控制器
#import "FactoryInfoBankCardListModel.h"// 银行卡信息model
#import "OrderSelectProductController.h"// 选择合作产品
@interface ShengChengFaHuoDanViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong) UITableView *listTableView;//列表
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) UILabel *commanyNameLab;// 厂家名称
@property (nonatomic, strong) UILabel *phoneLab;// 电话
@property (nonatomic, strong) UILabel *xiaDanTimeLab;// 下单时间
@property (nonatomic, strong) UILabel *adressLab;// 地址
@property (nonatomic, strong) UIButton *yiFuKuanBtn;// 订购方已付款
@property (nonatomic, strong) UIButton *weiFuKuanBtn;// 订购方未付款
@property (nonatomic, strong) WeiFaHuoOrderDetailModel *detailModel;
@property (nonatomic, strong) NSMutableArray *partsArray;// 产品列表数组
@property (nonatomic, strong) NSString *totalMoney;// 合计金额
@property (nonatomic, strong) SelectConditionTableView *zhangHuSelectView;// 收款账户选择视图
@property (nonatomic, strong) NSMutableArray *cardNameListArray;// 银行卡名称数组
@property (nonatomic, strong) NSMutableArray *bankCardsListModelArray;// 银行卡列表model
@property (nonatomic, strong) UIButton *zhangHuSelectBtn;// 请选择账户按钮
@property (nonatomic, strong) UITextField *wuLiuNameTF;// 物流公司输入框
@property (nonatomic, strong) SelectConditionTableView *wuLiuSelectView;// 这是一个选择视图，不是真正意义上的表
@property (nonatomic, strong) UITextField *danHaoTF;// 物流单号输入框
@property (nonatomic, strong) NSString *isPay;//是否付款 0否 1是 记录货物是否已付款的状态
@property (nonatomic, strong) NSString *bankCardId;// 银行卡id
@property (nonatomic, strong) NSString *logistics_code;// 快递编码 （顺丰 sf）
@property (nonatomic, strong) NSString *logistics_name;// 快递名称 顺丰
// 处理点击订购方是否已付款界面UI变动
@property (nonatomic, strong) UILabel *zhangHuLab;
@property (nonatomic, strong) UIView *line2;
@property (nonatomic, strong) UIButton *jianTouBtn;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *line3;
@property (nonatomic, strong) UILabel *danHaoLab;
@property (nonatomic, strong) UIView *line4;
@property (nonatomic, strong) UILabel *wuLiuLab;
@property (nonatomic, strong) UIView *line5;
// 请求收款账户用到的属性
@property (nonatomic, strong) FactoryInfoBankCardListModel *bankModel;
@property (nonatomic, strong) NSString *userToken;
@property (nonatomic, strong) UIImageView *diZhiImageView;
@property (nonatomic, strong) UILabel *promptLabel;

@end

static NSString *const ChanPinXingHaoTopViewCellID = @"ChanPinXingHaoTopViewCell";
static NSString *const ShengChengFaHuoDanListCellID = @"ShengChengFaHuoDanListCell";
static NSString *const ChanPinXingHaoTotalMoneyCellID = @"ChanPinXingHaoTotalMoneyCell";

@implementation ShengChengFaHuoDanViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = Main_BackGroundColor;
    self.isPay = @"0";
    self.userToken = @"";
    [self requestUserToken];
    [self requestGetUnSendPartsOrdersInfo];
    [self requestUserInfoData];
}

- (void)requestUserToken{
    [NetWorkTool POST:getUserToken param:nil success:^(id dic) {
        self.userToken = dic[@"data"];
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

// 请求收款账户信息
- (void)requestUserInfoData{
    [NetWorkTool POST:getFactoryInfo param:nil success:^(id dic) {
        self.bankModel = [FactoryInfoBankCardListModel mj_objectWithKeyValues:dic[@"data"][@"bankCard"]];
        NSDictionary *bankCardDic = dic[@"data"][@"bankCard"];
        // 防止界面上出现null
        if ([bankCardDic isKindOfClass:[NSNull class]] || [bankCardDic isEqual:[NSNull null]] || bankCardDic == nil) {
            [self.zhangHuSelectBtn setTitle:@"请选择账户" forState:UIControlStateNormal];
        } else {
            // 银行名字+ 账号
            NSString *str = [NSString stringWithFormat:@"%@ %@",self.bankModel.bank_name,self.bankModel.card_no];
            [self.zhangHuSelectBtn setTitle:str forState:UIControlStateNormal];
            self.bankCardId = self.bankModel.id;
        }
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

// 获取未发货订单详情
-(void)requestGetUnSendPartsOrdersInfo{
    NSDictionary *param = @{
        @"id":NOTNIL(self.id),// 产品种类id
        @"orderId":NOTNIL(self.orderId)// 订单Id
    };
    [self.listArray removeAllObjects];
    [NetWorkTool POST:getUnSendPartsOrdersInfo param:param success:^(id dic) {
        self.detailModel = [WeiFaHuoOrderDetailModel mj_objectWithKeyValues:dic[@"data"]];
        self.partsArray = [WeiFaHuoDetailPartsListModel mj_objectArrayWithKeyValuesArray:dic[@"data"][@"parts"]];
        // 初始总金额
        NSDecimalNumber *kaiShiMoney = [NSDecimalNumber decimalNumberWithString:@"0"];
        // 自己算总金额
        for (WeiFaHuoDetailPartsListModel *model in self.partsArray) {
            NSString *sale = [NSString stringWithFormat:@"%.2f",[model.price doubleValue]];
            NSDecimalNumber *count = [NSDecimalNumber decimalNumberWithString:model.count];
            NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:sale];
            //乘法运算函数  decimalNumberByMultiplyingBy
            NSDecimalNumber *money = [count decimalNumberByMultiplyingBy:price];
            //加法运算函数  decimalNumberByAdding
            kaiShiMoney =  [money decimalNumberByAdding:kaiShiMoney];
            NSString *total = [NSString stringWithFormat:@"%.2f",[[kaiShiMoney stringValue] doubleValue]];
            self.totalMoney = total;
        };
        
        [self addTopAddressView];
        [self addDownTableView];
        [self.listTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        
    } fail:^(NSError *error) {
        
    } needUser:NO];
}


-(void)leftBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addTopAddressView{
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 109)];
    backImageView.image = [UIImage imageNamed:@"导航背景"];
    [self.view addSubview:backImageView];
    
    UIButton *leftbutton = [[UIButton alloc] initWithFrame:CGRectMake(20, 35, 30, 30)];
    leftbutton.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftbutton setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [leftbutton setImage:imgname(@"leftWhiteBtn") forState:(UIControlStateNormal)];
    [leftbutton setTitleColor:KTEXTCOLOR forState:UIControlStateNormal];
    [leftbutton addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftbutton];
    
    UILabel *titlelab = [[UILabel alloc]initWithFrame:CGRectMake((ScreenW-100)/2, 40, 100, 15)];
    titlelab.font = FontSize(15);
    titlelab.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    titlelab.text = @"生成发货单";
    titlelab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titlelab];
    
    UIImageView *diZhiImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 64, ScreenW-40, 127)];
    diZhiImageView.image = [UIImage imageNamed:@"样式"];
    [self.view addSubview:diZhiImageView];
    self.diZhiImageView = diZhiImageView;
    
    //厂家名称
    self.commanyNameLab = [[UILabel alloc]init];
    self.commanyNameLab.frame = CGRectMake(21.5, 25.5, 56, 14);
    self.commanyNameLab.font = FontSize(14);
    self.commanyNameLab.textColor = [UIColor colorWithHexString:@"#333333"];
    self.commanyNameLab.text = self.detailModel.name;
    [diZhiImageView addSubview:self.commanyNameLab];
    
    //厂家 电话号
    self.phoneLab = [[UILabel alloc]init];
    self.phoneLab.frame = CGRectMake(self.commanyNameLab.right + 15, 27, 90, 12);
    self.phoneLab.font = FontSize(12);
    self.phoneLab.textColor = [UIColor colorWithHexString:@"#999999"];
    self.phoneLab.text = self.detailModel.phone;
    [diZhiImageView addSubview:self.phoneLab];
    
    //下单时间
    self.xiaDanTimeLab = [[UILabel alloc]init];
    self.xiaDanTimeLab.frame = CGRectMake(21.5, self.commanyNameLab.bottom + 15, diZhiImageView.width - 40, 12);
    self.xiaDanTimeLab.font = FontSize(12);
    self.xiaDanTimeLab.textColor = [UIColor colorWithHexString:@"#999999"];
    self.xiaDanTimeLab.text = [NSString stringWithFormat:@"下单时间：%@",self.detailModel.createTime];
    [diZhiImageView addSubview:self.xiaDanTimeLab];
    
    //地址 图片
    UIImageView *mapImage = [[UIImageView alloc]init];
    mapImage.frame = CGRectMake(19, self.xiaDanTimeLab.bottom +7.5, 17, 20);
    mapImage.image = [UIImage imageNamed:@"地址"];
    [diZhiImageView addSubview:mapImage];
    
    //地址lab
    NSString *addressStr = [NSString stringWithFormat:@"%@%@", @"收货地址: ",self.detailModel.address];
    CGFloat height = [NSString heightWithWidth:diZhiImageView.width - 60 fontNameString:@"PingFangSC-Medium" fontSize:11 text:addressStr];
    UILabel *adressLab = [[UILabel alloc]initWithFrame:CGRectMake(mapImage.right + 3, self.xiaDanTimeLab.bottom +7.5, diZhiImageView.width - 60, height)];
    adressLab.font = FontSize(11);
    adressLab.textColor = [UIColor colorWithHexString:@"#999999"];
    adressLab.text = addressStr;
    adressLab.numberOfLines = 0;
    [diZhiImageView addSubview:adressLab];
    self.adressLab = adressLab;
    
    
    diZhiImageView.frame = CGRectMake(20, 64, ScreenW-40, self.adressLab.bottom + 20);
    
    // 温馨提示
    UILabel *promptLabel = [[UILabel alloc] init];
    promptLabel.frame = CGRectMake(30, diZhiImageView.bottom, KWIDTH - 60, 20);
    promptLabel.text = @"温馨提示：您可以根据实际发货，修改预订数量和价格";
    promptLabel.textColor = [UIColor colorWithHexString:@"#FF0000"];
    promptLabel.font = [UIFont systemFontOfSize:10];
    [self.view addSubview:promptLabel];
    self.promptLabel = promptLabel;
    
}

-(void)addDownTableView{
    [self.listTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ChanPinXingHaoTopViewCell class]) bundle:nil] forCellReuseIdentifier:ChanPinXingHaoTopViewCellID];
    [self.listTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ShengChengFaHuoDanListCell class]) bundle:nil] forCellReuseIdentifier:ShengChengFaHuoDanListCellID];
    [self.listTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ChanPinXingHaoTotalMoneyCell class]) bundle:nil] forCellReuseIdentifier:ChanPinXingHaoTotalMoneyCellID];
    [self.view addSubview:self.listTableView];
    [self addTableBottomView];
}

- (UITableView *)listTableView
{
    if (!_listTableView) {
        _listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,self.promptLabel.bottom + 10, KWIDTH, KHEIGHT-kNaviHeight-self.diZhiImageView.height - 20) style:UITableViewStylePlain];
        _listTableView.hidden = NO;
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _listTableView.estimatedSectionHeaderHeight = 0;
        _listTableView.estimatedSectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = kColorWithHex(0xf5f5f5);
    }
    return _listTableView;
}

#pragma mark UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = Main_BackGroundColor;
    return headerView;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = Main_BackGroundColor;
    return headerView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==1) {
        return self.partsArray.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        ChanPinXingHaoTopViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ChanPinXingHaoTopViewCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section==1) {
        WeiFaHuoDetailPartsListModel *listModel  = [self.partsArray safeObjectAtIndex:indexPath.row];
        ShengChengFaHuoDanListCell *cell = [tableView dequeueReusableCellWithIdentifier:ShengChengFaHuoDanListCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.faHuoNumTFEndHandle = ^(NSString * _Nonnull text) {
            
            listModel.count = text;
            [self countTotalMoney];
        };
        cell.priceChangedEndHandle = ^(NSString * _Nonnull text) {
            listModel.price = text;
            [self countTotalMoney];
        };
        cell.model = listModel;
        [cell.deleteButton addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        cell.deleteButton.tag = indexPath.row;
        return cell;
    }
    if (indexPath.section==2) {
        ChanPinXingHaoTotalMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:ChanPinXingHaoTotalMoneyCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.totalMoney = self.totalMoney;
        cell.model = self.detailModel;
        [cell.addProductButton addTarget:self action:@selector(addProductButtonClick) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    NSString *cellIdentifier = @"iden3";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (void)addProductButtonClick{
    OrderSelectProductController * vc = [[OrderSelectProductController alloc] init];
    NSMutableArray *attributesArray = [NSMutableArray array];
    for (WeiFaHuoDetailPartsListModel *model in self.partsArray) {
        [attributesArray addObject:model.attribute];
    }
    NSString *attributes = [attributesArray componentsJoinedByString:@","];
    vc.attributes = attributes;
    vc.SelectProductModelArrayClick = ^(NSMutableArray * _Nonnull selectArray) {
        for (int i = 0; i< selectArray.count; i++) {
            WeiFaHuoDetailPartsListModel *model = selectArray[i];
            model.count = @"1";
        }
        [self.partsArray addObjectsFromArray:selectArray];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:1];
        [self.listTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
        [self countTotalMoney];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)deleteButtonClick:(UIButton *)sender{
    WeiFaHuoDetailPartsListModel *listModel  = [self.partsArray safeObjectAtIndex:sender.tag];
    [self.partsArray removeObject:listModel];
    
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:1];
    [self.listTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
    [self countTotalMoney];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0) {
        return 40;
    }
    if (indexPath.section ==1) {
        
        return 75;
    }
    if (indexPath.section ==2) {
        return 90;
    }
    return 0;
}

-(void)countTotalMoney{
    NSDecimalNumber *kaiShiMoney = [NSDecimalNumber decimalNumberWithString:@"0"];
    for (WeiFaHuoDetailPartsListModel *model in self.partsArray) {
        
        if ([model.price isEqualToString:@""]) {
            model.price = @"0";
        }
        if ([model.count isEqualToString:@""]) {
            model.count = @"0";
        }
        NSString *sale = [NSString stringWithFormat:@"%.2f",[model.price doubleValue]];
        NSDecimalNumber *count = [NSDecimalNumber decimalNumberWithString:model.count];
        NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:sale];
        //乘法运算函数  decimalNumberByAdding
        NSDecimalNumber *money = [count decimalNumberByMultiplyingBy:price];
        //加法运算函数  decimalNumberByAdding
        kaiShiMoney = [kaiShiMoney decimalNumberByAdding:money];
        NSString *total = [NSString stringWithFormat:@"%.2f",[[kaiShiMoney stringValue] doubleValue]];
        NSLog(@"total----%@",total);
        self.totalMoney = total;
    };
    if (self.partsArray.count == 0) {
        self.totalMoney = @"0.00";
    }
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
    [self.listTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(void)yiFuKuanBtnClick:(UIButton *)button{
    self.isPay = @"1";
    [_yiFuKuanBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    [_weiFuKuanBtn setImage:[UIImage imageNamed:@"合作产品单选未选中"] forState:UIControlStateNormal];
    
    self.line2.height = 0;
    self.zhangHuLab.height = 0;
    self.zhangHuSelectBtn.height = 0;
    self.jianTouBtn.height = 0;
    self.zhangHuSelectBtn.hidden = YES;
    
    self.line3.frame = CGRectMake(0, self.zhangHuLab.bottom, ScreenW, 5);
    self.danHaoLab.frame = CGRectMake(21.5, self.line3.bottom, 67, 48);
    self.danHaoTF.frame = CGRectMake(self.danHaoLab.right+13.5, self.line3.bottom, ScreenW-20.5-58.5-15, 48);
    self.line4.frame = CGRectMake(0, self.danHaoLab.bottom, ScreenW, 0.5);
    self.wuLiuLab.frame = CGRectMake(21.5, self.line4.bottom, 67, 48);
    self.wuLiuNameTF.frame = CGRectMake(self.zhangHuLab.right+13, self.line4.bottom, ScreenW-67-13, 48);
    self.line5.frame = CGRectMake(0, self.wuLiuNameTF.bottom, ScreenW, 82);
    self.bottomView.frame = CGRectMake(0, 0, ScreenH, 265-36);
    self.listTableView.tableFooterView = self.bottomView;
    [self.listTableView reloadData];
    //    self.line2.hidden = YES;
    //    self.zhangHuLab.hidden = YES;
    //    self.jianTouBtn.hidden = YES;
}

-(void)weiFuKuanBtnClick:(UIButton *)button{
    self.isPay = @"0";
    [_yiFuKuanBtn setImage:[UIImage imageNamed:@"合作产品单选未选中"] forState:UIControlStateNormal];
    [_weiFuKuanBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    
    self.line2.height = 5;
    self.zhangHuLab.height = 36;
    self.zhangHuSelectBtn.height = 36;
    self.jianTouBtn.height = 30;
    self.zhangHuSelectBtn.hidden = NO;
    
    self.line3.frame = CGRectMake(0, self.zhangHuLab.bottom, ScreenW, 5);
    self.danHaoLab.frame = CGRectMake(21.5, self.line3.bottom, 67, 48);
    self.danHaoTF.frame = CGRectMake(self.danHaoLab.right+13.5, self.line3.bottom, ScreenW-20.5-58.5-15, 48);
    self.line4.frame = CGRectMake(0, self.danHaoLab.bottom, ScreenW, 0.5);
    self.wuLiuLab.frame = CGRectMake(21.5, self.line4.bottom, 67, 48);
    self.wuLiuNameTF.frame = CGRectMake(self.zhangHuLab.right+13, self.line4.bottom, ScreenW-67-13, 48);
    self.line5.frame = CGRectMake(0, self.wuLiuNameTF.bottom, ScreenW, 82);
    
    self.bottomView.frame = CGRectMake(0, 0, ScreenH, 265);
    self.listTableView.tableFooterView = self.bottomView;
    [self.listTableView reloadData];
    
}
//选择账户
-(void)zhangHuSelectBtnBtnClick:(UIButton *)button
{
    //    //获取银行卡列表
    //    [self requestGetFactoryBankCards];
    //跳转到银行卡管理界面
    BankCardManageViewController *vc = [[BankCardManageViewController alloc] init];
    vc.selectRowStyle = 1;
    vc.selectedModel = ^(BankCardManageModel * _Nonnull model) {
        
        NSString *str = [NSString stringWithFormat:@"%@ %@",model.bank_name,model.card_no];
        [self.zhangHuSelectBtn setTitle:str forState:UIControlStateNormal];
        self.bankCardId = model.id;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)requestGetFactoryBankCards{
    NSDictionary *param = @{
        
    };
    [self.bankCardsListModelArray removeAllObjects];
    [self.cardNameListArray removeAllObjects];
    [NetWorkTool POST:getFactoryBankCards param:param success:^(id dic) {
        self.bankCardsListModelArray = [FactoryBankCardsListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        /**
         枚举器是一种苹果官方推荐的更加面向对象的一种遍历方式，相比于for循环,它具有高度解耦、面向对象、使用方便等优势；
         类似于for循环
         */
        [self.bankCardsListModelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.cardNameListArray addObject:[obj valueForKey:@"bank_name"]];
        }];
        [self addZhangHuSelectView];
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

-(void)addZhangHuSelectView{
    if (_zhangHuSelectView) {
        [_zhangHuSelectView removeFromSuperview];
    }
    __weak typeof(self) weakSelf = self;
    _zhangHuSelectView = [[SelectConditionTableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) withConditionArray:self.cardNameListArray withSelectedTitle:@"选择银行账户"];
    __weak __typeof(&*_zhangHuSelectView)weakSelectView = _zhangHuSelectView;
    
    _zhangHuSelectView.selectedCondition = ^(NSString * _Nonnull title) {
        [weakSelf.bankCardsListModelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([title isEqualToString:[obj valueForKey:@"bank_name"]]) {
                self->_bankCardId = [obj valueForKey:@"id"];
            }
        }];
        [weakSelf.zhangHuSelectBtn setTitle:title forState:UIControlStateNormal];
        [weakSelectView removeFromSuperview];
    };
    [[UIApplication sharedApplication].keyWindow addSubview:_zhangHuSelectView];
}

//确认生成
-(void)queRenShengCheng:(UIButton *)button {
    NSMutableArray *attributesArray = [NSMutableArray array];
    NSMutableArray *countsArray = [NSMutableArray array];
    NSMutableArray *moneysArray = [NSMutableArray array];
    NSMutableArray *priceArray = [NSMutableArray array];
    NSMutableArray *partsIdArray = [NSMutableArray array];
    NSLog(@"%@",self.partsArray);
    if (self.partsArray.count == 0) {
        ShowToastWithText(@"请添加商品");
        return;
    }
    for (WeiFaHuoDetailPartsListModel *model in self.partsArray) {
        
        [attributesArray addObject:model.attribute];
        [countsArray addObject:model.count];
        [partsIdArray addObject:model.partsId];
        [priceArray addObject:model.price];
        
        if ([model.count intValue] ==0) {
            ShowToastWithText(@"发货数量不能为0");
            return;
        }
        if ([model.count isEqualToString:@""]) {
            ShowToastWithText(@"发货数量不能为空");
            return;
        }
        if ([model.price doubleValue] ==0) {
            ShowToastWithText(@"单价不能为空或为0");
            return;
        }
        if ([model.price isEqualToString:@""]) {
            ShowToastWithText(@"单价不能为空或为0");
            return;
        }
        NSString *sale = [NSString stringWithFormat:@"%.2f",[model.price doubleValue]];
        NSDecimalNumber *count = [NSDecimalNumber decimalNumberWithString:model.count];
        NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:sale];
        //乘法运算函数  decimalNumberByAdding
        NSDecimalNumber *money = [count decimalNumberByMultiplyingBy:price];
        NSString *totalMoney = [money stringValue];
        [moneysArray addObject:totalMoney];
    };
    
    NSString *attributes = [attributesArray componentsJoinedByString:@","];
    NSString *counts = [countsArray componentsJoinedByString:@","];
    NSString *moneys = [moneysArray componentsJoinedByString:@","];
    NSString *partsIds = [partsIdArray componentsJoinedByString:@","];
    NSString *prices = [priceArray componentsJoinedByString:@","];
    NSString *totalMoney = [NSString stringWithFormat:@"%@",self.totalMoney];
    
    if ([self.isPay isEqualToString:@"0"] && self.bankCardId.length==0) {
        ShowToastWithText(@"请选择收款账户");
        return;
    }
    self.danHaoTF.text = [self disable_emoji:self.danHaoTF.text];
    self.danHaoTF.text = [self filterSpaces:self.danHaoTF.text];
    
    self.wuLiuNameTF.text = [self disable_emoji:self.wuLiuNameTF.text];
    self.wuLiuNameTF.text = [self filterSpaces:self.wuLiuNameTF.text];
    
    // 物流单号和物流公司只要填写一个 另一个必须填
    if (self.danHaoTF.text.length > 0) {
        if (self.wuLiuNameTF.text.length==0 ||[self.wuLiuNameTF.text isEqualToString:@""]) {
            ShowToastWithText(@"请选择物流公司");
            return;
        }
    }
    if (self.wuLiuNameTF.text.length > 0) {
        if (self.danHaoTF.text.length==0 ||[self.danHaoTF.text isEqualToString:@""]) {
            ShowToastWithText(@"请填写物流单号");
            return;
        }
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"orderId"] = NOTNIL(self.orderId);
    param[@"attributes"] = NOTNIL(attributes);
    param[@"counts"] = NOTNIL(counts);
    param[@"moneys"] = NOTNIL(moneys);
    param[@"salePrice"] = NOTNIL(prices);
    param[@"amount"] = NOTNIL(totalMoney);
    param[@"isPay"] = NOTNIL(self.isPay);
    if ([self.isPay isEqualToString:@"0"]) {
        param[@"bankCardId"] = NOTNIL(self.bankCardId);
    }
    param[@"logistics_num"] = NOTNIL(self.danHaoTF.text);
    param[@"logistics_code"] = @"";
    param[@"logistics_name"] = NOTNIL(_wuLiuNameTF.text);
    param[@"partsIds"] = NOTNIL(partsIds);
    param[@"userToken"] = NOTNIL(self.userToken);

    [MBProgressHUD showHUDAddedTo:self.view  animated:YES];
    
    [NetWorkTool POST:createSnedOrder param:param success:^(id dic) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
     //   [self.navigationController popViewControllerAnimated:YES];
        [self.navigationController popToRootViewControllerAnimated:YES];
        //        if ([self.isPay isEqualToString:@"0"]) {
        //            //发送通知 刷新到历史发货单界面数据
        //            [[NSNotificationCenter defaultCenter] postNotificationName:shengChengFaHuoDanWeiFuKuanSucccessed object:nil];
        //            [self.navigationController popViewControllerAnimated:YES];
        //        }else{
        //            //发送通知 刷新到历史发货单界面数据
        //            [[NSNotificationCenter defaultCenter] postNotificationName:shengChengFaHuoDanSucccessed object:nil];
        //            [self.navigationController popViewControllerAnimated:YES];
        //        }
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}

-(void)addTableBottomView{
    self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,265)];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 9.5)];
    line1.backgroundColor = Main_BackGroundColor;
    [self.bottomView addSubview:line1];
    
    //货物是否已付款
    UILabel *selectLab = [[UILabel alloc]initWithFrame:CGRectMake(21.5, line1.bottom+11, 100, 13)];
    selectLab.font = FontSize(13);
    selectLab.textColor = [UIColor colorWithHexString:@"#333333"];
    selectLab.text = @"货物是否已付款:";
    [self.bottomView addSubview:selectLab];
    
    //货物是否已付款
    _yiFuKuanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_yiFuKuanBtn setImage:[UIImage imageNamed:@"合作产品单选未选中"] forState:UIControlStateNormal];
    //_yiFuKuanBtn.frame = CGRectMake(selectLab.right+25, 14, 30, 30);
    _yiFuKuanBtn.frame = CGRectMake(selectLab.right+10, 14, 30, 30);
    [_yiFuKuanBtn addTarget:self action:@selector(yiFuKuanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView addSubview:_yiFuKuanBtn];
    
    UILabel *yiFuKuanLab = [[UILabel alloc]initWithFrame:CGRectMake(_yiFuKuanBtn.right, line1.bottom+12.5, 74, 12)];
    yiFuKuanLab.font = FontSize(12);
    yiFuKuanLab.textColor = [UIColor colorWithHexString:@"#999999"];
    yiFuKuanLab.text = @"订购方已付款";
    [self.bottomView addSubview:yiFuKuanLab];
    
    //订购方未付款
    _weiFuKuanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_weiFuKuanBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    _weiFuKuanBtn.frame = CGRectMake(yiFuKuanLab.right+19, 14, 30, 30);
    [_weiFuKuanBtn addTarget:self action:@selector(weiFuKuanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView addSubview:_weiFuKuanBtn];
    
    UILabel *weiFuKuanLab = [[UILabel alloc]initWithFrame:CGRectMake(_weiFuKuanBtn.right, line1.bottom+12.5, 74, 12)];
    weiFuKuanLab.font = FontSize(12);
    weiFuKuanLab.textColor = [UIColor colorWithHexString:@"#999999"];
    weiFuKuanLab.text = @"订购方未付款";
    [self.bottomView addSubview:weiFuKuanLab];
    
    self.line2 = [[UIView alloc]initWithFrame:CGRectMake(0, selectLab.bottom+12.5, ScreenW, 5)];
    self.line2.backgroundColor = Main_BackGroundColor;
    [self.bottomView addSubview:self.line2];
    
    //收款账户：
    self.zhangHuLab = [[UILabel alloc]initWithFrame:CGRectMake(21.5, self.line2.bottom, 67, 36)];
    self.zhangHuLab.font = FontSize(13);
    self.zhangHuLab.textColor = [UIColor colorWithHexString:@"#333333"];
    self.zhangHuLab.text = @"收款账户：";
    [self.bottomView addSubview:self.zhangHuLab];
    
    //账户 选择银行卡号
    _zhangHuSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _zhangHuSelectBtn.frame = CGRectMake(self.zhangHuLab.right+13, self.line2.bottom, ScreenW-67-13, 36);
    _zhangHuSelectBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_zhangHuSelectBtn setTitle:@"请选择账户" forState:UIControlStateNormal];
    [_zhangHuSelectBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    _zhangHuSelectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [_zhangHuSelectBtn addTarget:self action:@selector(zhangHuSelectBtnBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView addSubview:_zhangHuSelectBtn];
    
    self.jianTouBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.jianTouBtn.frame = CGRectMake(ScreenW-30, self.line2.bottom+3, 30, 30);
    [self.jianTouBtn setImage:[UIImage imageNamed:@"grayRightArrow"] forState:UIControlStateNormal];
    [self.jianTouBtn addTarget:self action:@selector(zhangHuSelectBtnBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView addSubview:self.jianTouBtn];
    
    self.line3 = [[UIView alloc]initWithFrame:CGRectMake(0, self.zhangHuLab.bottom, ScreenW, 5)];
    self.line3.backgroundColor = Main_BackGroundColor;
    [self.bottomView addSubview:self.line3];
    
    //物流单号：
    self.danHaoLab = [[UILabel alloc]initWithFrame:CGRectMake(21.5, self.line3.bottom, 67, 48)];
    self.danHaoLab.font = FontSize(13);
    self.danHaoLab.textColor = [UIColor colorWithHexString:@"#333333"];
    self.danHaoLab.text = @"物流单号：";
    [self.bottomView addSubview:self.danHaoLab];
    
    self.danHaoTF = [[UITextField alloc]initWithFrame:CGRectMake(self.danHaoLab.right+13.5, self.line3.bottom, ScreenW-20.5-58.5-15, 48)];
    self.danHaoTF.placeholder = @"请填写物流单号";
    self.danHaoTF.textColor = [UIColor colorWithHexString:@"#333333"];
    self.danHaoTF.font = [UIFont systemFontOfSize:12];
    self.danHaoTF.keyboardType = UIKeyboardTypeNumberPad;
    self.danHaoTF.delegate = self;
    [self.bottomView addSubview:self.danHaoTF];
    
    self.line4 = [[UIView alloc]initWithFrame:CGRectMake(0, self.danHaoLab.bottom, ScreenW, 0.5)];
    self.line4.backgroundColor = Main_BackGroundColor;
    [self.bottomView addSubview:self.line4];
    
    //物流公司
    self.wuLiuLab = [[UILabel alloc]initWithFrame:CGRectMake(21.5, self.line4.bottom, 67, 48)];
    self.wuLiuLab.font = FontSize(13);
    self.wuLiuLab.textColor = [UIColor colorWithHexString:@"#333333"];
    self.wuLiuLab.text = @"物流公司:";
    [self.bottomView addSubview:self.wuLiuLab];
    
    _wuLiuNameTF = [[UITextField alloc]initWithFrame:CGRectMake(self.zhangHuLab.right+13, self.line4.bottom, ScreenW-67-13, 48)];
    _wuLiuNameTF.font = [UIFont systemFontOfSize:12];
    _wuLiuNameTF.textColor = [UIColor colorWithHexString:@"#333333"];
    _wuLiuNameTF.placeholder = @"请填写物流公司";
    _wuLiuNameTF.keyboardType = UIKeyboardTypeNamePhonePad;
    _wuLiuNameTF.delegate = self;
    [self.bottomView addSubview:_wuLiuNameTF];
    
    self.line5 = [[UIView alloc]initWithFrame:CGRectMake(0, _wuLiuNameTF.bottom, ScreenW, 82)];
    self.line5.backgroundColor = Main_BackGroundColor;
    [self.bottomView addSubview:self.line5];
    //确认生成
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake((ScreenW-125)/2, 20, 125, 36);
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"按钮背景"] forState:UIControlStateNormal];
    [sureBtn setTitle:@"确认发送" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    sureBtn.titleLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    [sureBtn addTarget:self action:@selector(queRenShengCheng:) forControlEvents:UIControlEventTouchUpInside];
    [self.line5 addSubview:sureBtn];
    
    self.listTableView.tableFooterView = self.bottomView;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.danHaoTF) {
        BOOL isHave = [CommonMethod isHaveChineseInString:string];
        if (isHave == YES) {
            ShowToastWithText(@"不能输入汉字");
            return NO;
        }else{
            return YES;
        }
    }else{
        return YES;
    }
}
// 过滤空格
- (NSString *)filterSpaces:(NSString *)text{
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n"withString:@""];
    return text;
}
/**
 *  过滤字符串中的emoji
 */
- (NSString *)disable_emoji:(NSString *)text{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}

-(NSMutableArray *)cardNameListArray{
    if (!_cardNameListArray) {
        _cardNameListArray = [NSMutableArray array];
    }
    return _cardNameListArray;
}

-(NSMutableArray *)bankCardsListModelArray{
    if (!_bankCardsListModelArray) {
        _bankCardsListModelArray = [NSMutableArray array];
    }
    return _bankCardsListModelArray;
}

@end
